<template>
  <div class="subscribe-label" v-loading="loading">
    <div class="">
      <p class="mb">热门标签推荐：</p>
      <div class="sl-con">
        <el-button
          class="sl-btn"
          v-for="item in hotList"
          :key="item.name"
          type="primary"
          round
          size="small"
          :class="[formInfo.hotL.indexOf(item.name) > -1 ? 'active' : '']"
          @click="handleClick(item, 'hot')"
          >{{ item.name }}</el-button
        >
        <!-- <el-button
          class="sl-btn"
          icon="el-icon-plus"
          round
          size="small"
          @click="handleAddTag('hot')"
          >添加标签</el-button
        > -->
      </div>
    </div>
    <div class="">
      <p class="sl-tip">按媒体推荐：</p>
      <div class="sl-con">
        <el-button
          class="sl-btn"
          v-for="item in mediaList"
          :key="item.name"
          type="primary"
          round
          size="small"
          :class="[formInfo.mediaL.indexOf(item.name) > -1 ? 'active' : '']"
          @click="handleClick(item, 'media')"
          >{{ item.name }}</el-button
        >
        <!-- <el-button
          class="sl-btn"
          icon="el-icon-plus"
          round
          size="small"
          @click="handleAddTag('media')"
          >添加标签</el-button
        > -->
      </div>
    </div>
    <div class="">
      <p class="sl-tip">订阅推送时间：</p>
      <div class="sl-wrapper flex">
        <div>
          <span>推送频率：</span>
          <el-select
            class="long-select"
            v-model="formInfo.pushFrequency"
            placeholder="请选择推送频率"
            multiple
          >
            <el-option
              v-for="item in frequencyList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <div>
          <span>推送时间：</span>
          <el-time-select
            class="short"
            v-model="formInfo.pushTime"
            :picker-options="{
              start: '05:30',
              step: '00:15',
              end: '18:30',
            }"
            placeholder="请选择"
          >
          </el-time-select>
        </div>
      </div>
    </div>
    <div class="btn-con">
      <el-button plain @click="handleClose">取消返回</el-button>
      <el-button type="primary" @click="handleSave">保存订阅</el-button>
    </div>
    <!-- 添加标签 -->
    <el-dialog
      title="添加标签"
      :visible.sync="dialogVisible"
      width="400px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
      >
        <el-form-item label="标签名称" prop="tagName">
          <el-input
            v-model="ruleForm.tagName"
            placeholder="请输入标签名称"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    tagsInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      loading: false,
      dialogVisible: false,
      tagName: "",
      frequencyList: [
        {
          label: "星期一",
          value: "MON",
        },
        {
          label: "星期二",
          value: "TUE",
        },
        {
          label: "星期三",
          value: "WED",
        },
        {
          label: "星期四",
          value: "THU",
        },
        {
          label: "星期五",
          value: "FRI",
        },
        {
          label: "星期六",
          value: "SAT",
        },
        {
          label: "星期日",
          value: "SUN",
        },
      ],
      hotList: [],
      mediaList: [],
      formInfo: {
        hot: [],
        hotL: [],
        media: [],
        mediaL: [],
        pushFrequency: [],
        pushTime: "07:00",
      },
      ruleForm: {
        tagName: "",
      },
      rules: {
        tagName: [
          { required: true, message: "请输入标签名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      let ti = this.tagsInfo;
      this.hotList = ti.popularTagsObj.popularTags;
      this.formInfo.hot = ti.popularTagsObj.userPopularTags || [];
      this.formInfo.hot.map((i) => {
        this.formInfo.hotL.push(i.name);
      });
      this.mediaList = ti.mediaTagsObj.mediaTags;
      this.formInfo.media = ti.mediaTagsObj.userMediaTags || [];
      this.formInfo.media.map((i) => {
        this.formInfo.mediaL.push(i.name);
      });
      this.formInfo.pushFrequency = ti.pushFrequency.split(",");
      this.formInfo.pushTime = ti.pushTime;
    },
    handleAddTag(type) {
      this.dialogVisible = true;
      // this.tagName = "";
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //
        } else {
          return false;
        }
      });
    },
    handleClick(item, type) {
      let index = this.formInfo[type + "L"].indexOf(item.name);
      // console.log("index:", index);
      if (index > -1) {
        this.formInfo[type].splice(index, 1);
        this.formInfo[type + "L"].splice(index, 1);
      } else {
        this.formInfo[type].push(item);
        this.formInfo[type + "L"].push(item.name, 1);
      }
    },
    handleClose(type = "") {
      this.$emit("close", type);
    },
    handleSave() {
      this.loading = true;
      const userInfo = JSON.parse(localStorage.getItem("homeUserInfo"));
      let fi = this.formInfo;
      let timeDate = fi.pushTime.split(":");
      let cron = `* ${timeDate[1] == 0 ? 0 : timeDate[1]} ${parseInt(
        timeDate[0]
      )} ? * ${fi.pushFrequency.join(",")} *`;
      // return;
      let param = {
        userId: userInfo.id,
        pushFrequency: fi.pushFrequency.join(","),
        pushTime: fi.pushTime,
        cron: cron,
        mediaTags: this.mediaList,
        userMediaTags: fi.media,
        popularTags: this.hotList,
        userPopularTags: fi.hot,
      };
      this.$post(this.$Url.personal.saveSubscribeTags, param).then((res) => {
        this.loading = false;
        this.$message({
          message: "订阅成功",
          type: "success",
        });
        this.handleClose("refresh");
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.subscribe-label {
  .sl-tip {
    margin: 20px 0;
  }
  .sl-btn {
    margin: 0 20px 20px 0;
  }
  .active {
    background-color: $color120;
    border-color: $color120;
  }
  .mb {
    margin-bottom: 20px;
  }
  .sl-con {
    max-height: 300px;
    overflow-y: auto;
  }
  .sl-wrapper {
    margin: 20px 0 40px;
    justify-content: space-around;
  }
  .btn-con {
    text-align: center;
    .el-button {
      margin-right: 40px;
    }
  }
  .long-select {
    width: 450px;
  }
  .short {
    width: 100px;
  }
}
</style>
